module tool {
    export class GameWebSocket {
        public webSocket: WebSocket
        public static ip: string = "ws://183.60.233.248:16669";
        private _webSocketService: WebSocketService;
        private messageQuota: number = 5
        
        public constructor() {
            this.webSocket = new WebSocket(GameWebSocket.ip);
            this.webSocket.onopen = this.onSocketOpen;
            this.webSocket.onclose = this.onSocketClose;
            this.webSocket.onmessage = this.onSocketMessage;
            this.webSocket["that"] = this
            this._webSocketService = new WebSocketService(this.webSocket)
        }
        private static _sockete: GameWebSocket;
        public static getSocket(): GameWebSocket {
            if (GameWebSocket._sockete == null) {
                GameWebSocket._sockete = new GameWebSocket()
            }
            return GameWebSocket._sockete;
        }
        private onSocketOpen(e): void {
            console.log(e)
            var that = (e.currentTarget as WebSocket)["that"];
            window.setInterval(function () {
                if (that) {
                    that._webSocketService.sendUpdate()
                }

            }, 500)
            if(that){
                 that.socketService.dispatchEventWith(WebSocketService.SOCKET_CONNECT);
            }
           
            // this._webSocketService.sendUpdate()
            /*var uri = this.parseUri(document.location)
            if (uri.queryKey.oauth_token) {
                this.authorize(uri.queryKey.oauth_token, uri.queryKey.oauth_verifier)
            }*/
        }
        public get socketService(): WebSocketService {
            return this._webSocketService;
        }
        private parseUri(str: string): any {
            var o = {
                strictMode: false,
                key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
                q: {
                    name: "queryKey",
                    parser: /(?:^|&)([^&=]*)=?([^&]*)/g
                },
                parser: {
                    strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
                    loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
                }
            }
            var m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
                uri = {},
                i = 14;
            while (i--) uri[o.key[i]] = m[i] || "";
            uri[o.q.name] = {};
            uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
                if ($1) uri[o.q.name][$1] = $2;
            });
            return uri;
        };
        private onSocketClose(e): void {
             var that = (e.currentTarget as WebSocket)["that"];
             if(that){
                  that._webSocketService.connectionClosed();
             }
           
        }
        public onSocketMessage(e: any): void {
            try {
                var that = (e.currentTarget as WebSocket)["that"];
                if (that) {
                    if (e.data == "@") {
                       // that._webSocketService.processMessage(e.data);
                    } else {
                        console.log(e.data);
                        var data = JSON.parse(e.data);
                        that._webSocketService.processMessage(data);
                    }
                }
            } catch (e) { 
                console.error(e);
            }
        }
        private authorize(token, verifier) {
            this._webSocketService.authorize(token, verifier);
        }
        public sendMessage(msg) {
            console.log(msg,"========发送========");
            this._webSocketService.sendMessage(msg);
        }
    }
}